<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2015/12/22
 * Time: 20:40
 */

namespace App\Http\Controllers\Api;
use Illuminate\Support\Facades\Request;
use App\Http\Controllers\Controller;
use BaseClass\Component\Article\ArticleLabel;

class LabelController extends Controller
{
    //查找专题
    public function sLabel()
    {
        /*
          * $limit
          * |-user   按照用户筛选
          * |-sort   排序方式
          * |-num    每页条数
          * |-start  起始条数
          * |-desc   是否倒序列
          * |-article    关联文章查找
          * |-search     关键词查找
          *
          * $return_data
          * |-status 是否成功
          * |-message 消息
          * |-data   数据 DB返回的二维结构
          *
          */
        $query_limit = Request::input("query_limit");
        //$articleLabel = new ArticleLabel(null);
        $labelArray = ArticleLabel::select($query_limit);

        return response()->json($labelArray);
    }
    //添加标签
    public function aLabel()
    {
        $articleLabel = new ArticleLabel(null);
        $info_array['label_name'] = Request::input("label_name");
        if($articleLabel->add($info_array))
        {
            return response()->json(["status"=>true,"message"=>"添加标签成功"]);
        }
        else
        {
            return response()->json(["status"=>true,"message"=>"添加标签失败"]);
        }
    }
    //更新标签
    public function uLabel()
    {
        $label_id = Request::input("label_id");
        $info_array['label_name'] = Request::input("label_name");
        $articleLabel = new ArticleLabel($label_id);
        if($articleLabel->update($info_array))
        {
            return response()->json(["status"=>true,"message"=>"标签更新成功"]);
        }
        else
        {
            return response()->json(["status"=>true,"message"=>"标签更新失败"]);
        }
    }
    //删除标签
    public function dLabel()
    {
        $label_id = Request::input("label_id");
        $articleLabel = new ArticleLabel($label_id);
        if($articleLabel->delete())
        {
            return response()->json(["status"=>true,"message"=>"删除成功"]);
        }
        else
        {
            return response()->json(["status"=>true,"message"=>"删除失败"]);
        }
    }
    //给文章添加标签
    public function addArticleLabel()
    {
        //添加标签，需要传入文章ID和标签ID
        $label_id = Request::input("label_id");
        $article_id = Request::input("article_id");
        $articleLabel = new ArticleLabel($label_id);
        if($articleLabel->aAticleLabel($article_id))
        {
            return response()->json(["status"=>true,"message"=>"添加标签成功"]);
        }
        else
        {
            return response()->json(["status"=>true,"message"=>"添加标签失败"]);
        }
    }
    //移除标签
    public function removeArticleLabel()
    {
        //添加标签，需要传入文章ID和标签ID
        $label_id = Request::input("label_id");
        $article_id = Request::input("article_id");
        $articleLabel = new ArticleLabel($label_id);
        if($articleLabel->removeLabel($article_id))
        {
            return response()->json(["status"=>true,"message"=>"移除标签成功"]);
        }
        else
        {
            return response()->json(["status"=>true,"message"=>"移除标签失败"]);
        }
    }
}